package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.contentshow.Details;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.databinding.QuintessenceShowItemBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/6 20:44
 * @projectName CoffeeLoverSupportLibrary
 * @className QuintessenceListAdapter.java
 * @description TODO
 */

public class QuintessenceListAdapter extends RecyclerView.Adapter {
    private List<Article> articles;

    public QuintessenceListAdapter(List<Article> articles) {
        this.articles = articles;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quintessence_show_item,parent,false);
        QuintessenceShowItemBinding quintessenceShowItemBinding=QuintessenceShowItemBinding.bind(view);
        return new QuintessenceArticleViewHolder(quintessenceShowItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Article article=articles.get(position);

        Glide.with(((QuintessenceArticleViewHolder)holder)
                .quintessenceShowItemBinding.getRoot().getContext())
                .load(article.getArticleBanner())
                .into(((QuintessenceArticleViewHolder)holder).quintessenceShowItemBinding.quintessenceItemImage);

        ((QuintessenceArticleViewHolder)holder)
        .quintessenceShowItemBinding.quintessenceItemContent.setText(article.getArticleTitle());


        ((QuintessenceArticleViewHolder)holder)
                .quintessenceShowItemBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(holder.itemView.getContext(), Details.class);
                intent.putExtra("articleId",article.getArticleId());
                intent.putExtra("title",article.getArticleTitle());
                ((QuintessenceArticleViewHolder)holder)
                        .quintessenceShowItemBinding
                        .getRoot()
                        .getContext()
                        .startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.e("Tag",String.valueOf(articles.size()));
        if(0!=articles.size()){
            return articles.size();
        }
        return 10;
    }

    static class QuintessenceArticleViewHolder extends RecyclerView.ViewHolder{
        QuintessenceShowItemBinding quintessenceShowItemBinding;
        public QuintessenceArticleViewHolder(@NonNull QuintessenceShowItemBinding quintessenceShowItemBinding) {
            super(quintessenceShowItemBinding.getRoot());
            this.quintessenceShowItemBinding=quintessenceShowItemBinding;
        }
    }

}



